package com.newer.utils;

import org.apache.commons.lang.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by well on 15/3/19.
 */
@SuppressWarnings("static-access")
public class DateUtil {

	/**
	 * 年月日时分秒(无下划线) yyyyMMddHHmmss
	 */
	public static final String dtLong = "yyyyMMddHHmmss";
	/**
	 * 年月日时分秒毫秒(无下划线) yyyyMMddHHmmssffff
	 */
	public static final String dtToLong = "yyyyMMddHHmmssSSS";

	/**
	 * 完整时间 yyyy-MM-dd HH:mm:ss
	 */
	public static final String simple = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 年月日(无下划线) yyyyMMdd
	 */
	public static final String dtShort = "yyyyMMdd";

	/**
	 * 年月日(无下划线) yyyyMMdd
	 */
	public static final String dtShortFC = "yyyy/MM/dd";

	public static final String dtShortChina = "yyyy年MM月dd日";

	public static final String dtMonthChina = "MM月dd日";

	/**
	 * 年月日(无下划线) yyyyMMdd
	 */
	public static final String dtShortSimple = "yyyy-MM-dd";
	public static final String year = "yyyy";
	public static final String month = "MM";
	public static final String day = "dd";
	public static final String hour = "HH";
	public static final String hours = "HH:mm";

	/**
	 * 返回系统当前时间(精确到毫秒),作为一个唯一的订单编号
	 *
	 * @return 以yyyyMMddHHmmssSSS为格式的当前系统时间
	 */
	public static String getDtToLong() {
		Date date = new Date();
		DateFormat df = new SimpleDateFormat(dtToLong);
		return df.format(date);
	}

	/**
	 * 获取系统当前日期(精确到毫秒)，格式：yyyy-MM-dd HH:mm:ss
	 *
	 * @return
	 */
	public static String getDateFormatter() {
		Date date = new Date();
		DateFormat df = new SimpleDateFormat(simple);
		return df.format(date);
	}

	/**
	 * 获取系统当期年月日(精确到天)，格式：yyyyMMdd
	 *
	 * @return
	 */
	public static String getDate() {
		Date date = new Date();
		DateFormat df = new SimpleDateFormat(dtShort);
		return df.format(date);
	}

	public static String getDate(Date date) {
		DateFormat df = new SimpleDateFormat(dtShort);
		return df.format(date);
	}

	public static String getDateChina(Date date) {
		DateFormat df = new SimpleDateFormat(dtShortChina);
		return df.format(date);
	}

	/**
	 * 获取系统当期年，格式：yyyy
	 *
	 * @return
	 */
	public static String getYear() {
		DateFormat df = new SimpleDateFormat(year);
		return df.format(new Date());
	}

	/**
	 * 获取系统当期年，格式：yyyy
	 *
	 * @return
	 */
	public static int getMonth() {
		DateFormat df = new SimpleDateFormat(month);
		return Integer.valueOf(df.format(new Date()));
	}

	/**
	 * 获取系统当期年，格式：yyyy
	 *
	 * @return
	 */
	public static String getHour(Date date) {
		DateFormat df = new SimpleDateFormat(hour);
		return String.valueOf(df.format(date));
	}

	/**
	 * 获取系统当期年，格式：yyyy
	 *
	 * @return
	 */
	public static int getHourInt(Date date) {
		DateFormat df = new SimpleDateFormat(hour);
		return Integer.parseInt(df.format(date));
	}

	/**
	 * 使用参数Format将字符串转为Date
	 */
	public static Date parseDate(String strDate, String pattern) throws ParseException {
		return StringUtils.isBlank(strDate) ? null : new SimpleDateFormat(pattern).parse(strDate);
	}

	/**
	 * 获得默认的 date pattern
	 */
	public static String getDatePattern() {
		return dtShortSimple;
	}

	/**
	 * 使用预设格式将字符串转为Date
	 */
	public static Date parseDate(String strDate) {
		return StringUtils.isBlank(strDate) ? null : parse(strDate, getDatePattern());
	}

	/**
	 * 获取系统当期年月日(精确到天)，格式：yyyyMMdd
	 *
	 * @return
	 */
	public static String getDateByFormart(Date date, String format) {
		DateFormat df = new SimpleDateFormat(format);
		return df.format(date);
	}

	public static Date parse(String date, String format) {
		if (StringUtils.isNotBlank(date)) {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			if (date != null) {
				try {
					return sdf.parse(date);
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}

	// 获取相差小时
	public static long dateDiff(Date date1, Date date2) {
		return (date1.getTime() - date2.getTime()) / (60 * 60 * 1000);
	}

	// 获取相差小时
	public static long dateDiffDay(Date date1, Date date2) {
		return (date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000);
	}

	// 获取相差小时
	public static long dateDiffMin(Date date1, Date date2, int min) {
		return (date1.getTime() - date2.getTime()) / (min * 60 * 1000);
	}

	// 获取相差小时
	public static long dateDiffMin(Date date1, Date date2) {
		return (date1.getTime() - date2.getTime()) / (60 * 1000);
	}

	public static Date getDateCurrentZero() {
		Date date = new Date();
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(date);
		Date date2 = new Date(date.getTime() - gc.get(gc.HOUR_OF_DAY) * 60 * 60 * 1000
				- gc.get(gc.MINUTE) * 60 * 1000 - gc.get(gc.SECOND) * 1000);
		return date2;
	}

	public static Date addDate(int day) {
		Date date = new Date();
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(date);
		Date date2 = new Date(date.getTime() - gc.get(gc.HOUR_OF_DAY) * 60 * 60 * 1000
				- gc.get(gc.MINUTE) * 60 * 1000 - gc.get(gc.SECOND) * 1000
				+ day * 24 * 60 * 60 * 1000);
		return date2;
	}

	public static Date addDate(Date date, int day) {
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(date);
		Date date2 = new Date(date.getTime() - gc.get(gc.HOUR_OF_DAY) * 60 * 60 * 1000
				- gc.get(gc.MINUTE) * 60 * 1000 - gc.get(gc.SECOND) * 1000
				+ day * 24 * 60 * 60 * 1000);
		return date2;
	}

	public static Date addDayNotZero(Date date, int day) {
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(date);
		gc.add(Calendar.DAY_OF_MONTH, +day);
		Date date2 = gc.getTime();
		return date2;
	}

	public static Date addHour(Date date, int hour) {
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(date);
		Date date2 = new Date(date.getTime() + hour * 60 * 60 * 1000);
		return date2;
	}

	public static Date addMin(Date date, int min) {
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(date);
		Date date2 = new Date(date.getTime() + min * 60 * 1000);
		return date2;
	}

	/**
	 * 获取当年的第一天
	 *
	 * @return
	 */
	public static Date getCurrYearFirst() {
		Calendar currCal = Calendar.getInstance();
		int currentYear = currCal.get(Calendar.YEAR);
		return getYearFirst(currentYear);
	}

	/**
	 * 获取某年第一天日期
	 *
	 * @param year 年份
	 * @return Date
	 */
	public static Date getYearFirst(int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		Date currYearFirst = calendar.getTime();
		return currYearFirst;
	}

	/**
	 * 获取当年的最后一天
	 *
	 * @return
	 */
	public static Date getCurrYearLast() {
		Calendar currCal = Calendar.getInstance();
		int currentYear = currCal.get(Calendar.YEAR);
		return getYearLast(currentYear);
	}

	/**
	 * 获取某年最后一天日期
	 *
	 * @param year 年份
	 * @return Date
	 */
	public static Date getYearLast(int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		calendar.roll(Calendar.DAY_OF_YEAR, -1);
		Date currYearLast = calendar.getTime();

		return currYearLast;
	}

	public static void main(String[] args0) {
		System.out.println(Integer.parseInt(getDateByFormart(new Date(), day)));
	}
}
